package com.redoor.app.stake.basicdata.hsf.service.impl;

import com.alibaba.boot.hsf.annotation.HSFProvider;
import com.redoor.app.stake.basicdata.model.controllerModel.BaseStationCriteria;
import com.redoor.app.stake.basicdata.model.constants.BasicDataRedisConstants;
import com.redoor.app.stake.basicdata.service.BaseStationService;
import com.yotouch.app.stake.hsf.StationService;
import com.yotouch.app.stake.model.Station;
import com.yotouch.app.stake.model.response.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * HSF接口服务实现类，整合二期资产接口代码以及返回类型
 * Created by wangjunlei on 2018/2/2.
 */
@HSFProvider(serviceInterface = StationService.class, serviceVersion = "5.0.0", serviceGroup = "grp_asset_of_stake", clientTimeout = 600000)
@Service
public class StationServiceImpl implements StationService {

    @Autowired
    private BaseStationService baseStationService;

    /**
     * 根据站uuid查询站下所有的正常使用的桩 stakestatus=3
     * 并将直流、交流信息汇总到站属性中
     * 站对象中包含桩列表
     * @apiNote  e充电使用
     * @param stationUuid
     * @return
     */
    @Deprecated
    @Override
    public ResponseStation countStakes(String stationUuid) {
        ResponseStation result = new ResponseStation(0,"");
        if(StringUtils.isEmpty(stationUuid)){
            result.setErrCode(-1);
            result.setErrMsg("station uuid is blank");
            return result;
        }
        Map<String, Object> operatorCount = baseStationService.qryStationStakeTypeCnt(stationUuid);
        ResponseObject object = new ResponseObject();
        object.setObject(operatorCount);
        result.setObject(object);
        return result;
    }

    /**
     * 统计国网站(isthird)正常状态站 stationstatus =3 数量
     *
     * @return
     */
    @Override
    public  ResponseInt sgccTotalCount() {
        List<Integer> stationStatusList = new ArrayList<Integer>();
        stationStatusList.add(BasicDataRedisConstants.STATION_STATUS_OPERATION);

        BaseStationCriteria params = new BaseStationCriteria();
        params.setStationstatus(stationStatusList);
        params.setIsthrids(BasicDataRedisConstants.THIRD_SGCC);

        int cnt = baseStationService.qryStationInfoCnt(params);
        ResponseInt result = new ResponseInt(0,"");
        result.setValue(cnt);

        return result;
    }

    /**
     * 获取国网(isthird)正常状态站 stationstatus =3 列表
     *
     * @param offset
     * @param limit
     * @return
     */
    @Override
    public  ResponseStationList sgccList(int offset, int limit) {
        ResponseStationList result = new ResponseStationList(0,"");
        if(limit > 100){
            result.setErrMsg("get station size too large , set default size 100") ;
            limit = 100 ;
        }
        List<Integer> stationStatusList = new ArrayList<Integer>();
        stationStatusList.add(BasicDataRedisConstants.STATION_STATUS_OPERATION);

        BaseStationCriteria params = new BaseStationCriteria();
        params.setStationstatus(stationStatusList);
        params.setIsthrids(BasicDataRedisConstants.THIRD_SGCC);
        params.setLimit(limit);
        params.setOffset(offset);

        List<Station> stationList = baseStationService.qryStationInfoDetailForHSF(params);

        result.setStationList(stationList);
        return result;
    }

    /**
     * 统计某状态下的所有站数量
     *
     * @param stationStatus 2 待投运,3 运行,8 维护,10 退运,11 停运
     */
    @Override
    public ResponseInt pueryTotaStatusCount(int stationStatus){
        List<Integer> stationStatusList = new ArrayList<Integer>();
        stationStatusList.add(stationStatus);

        BaseStationCriteria params = new BaseStationCriteria();
        params.setStationstatus(stationStatusList);
        params.setUrposetype(BasicDataRedisConstants.STATION_URPOSETYPE_PUBLIC);

        int cnt = baseStationService.qryStationInfoCnt(params);
        ResponseInt result = new ResponseInt(0,"");
        result.setValue(cnt);

        return result;
    }

    /**
     * 获取某状态下的所有站列表
     *
     * @param offset
     * @param limit
     * @param stationStatus 2 待投运,3 运行,8 维护,10 退运,11 停运
     * @return
     */
    @Override
    public ResponseStationList pueryStatusList(int offset, int limit, int stationStatus) {
        ResponseStationList result = new ResponseStationList(0,"");

        List<Integer> stationStatusList = new ArrayList<Integer>();
        stationStatusList.add(stationStatus);

        BaseStationCriteria params = new BaseStationCriteria();
        params.setStationstatus(stationStatusList);
        params.setUrposetype(BasicDataRedisConstants.STATION_URPOSETYPE_PUBLIC);

        if(limit > 100){
            result.setErrMsg("get station size too large , set default size 100") ;
            limit = 100 ;
        }
        params.setLimit(limit);
        params.setOffset(offset);

        List<Station> stationList = baseStationService.qryStationInfoDetailForHSF(params);

        result.setStationList(stationList);
        return result;
    }

    /**
     * 根据组织机构编码(provinceOrgCode or cityorgcode or partorgcode)获取所有本机构下的站以及下一级机构的站列表
     *
     * @param orgCode
     * @return
     */
    @Override
    public ResponseStationList listAllByOrg(String orgCode){

        BaseStationCriteria params = new BaseStationCriteria();
        params.setOrgCode(orgCode);

        params.setLimit(0);
        params.setOffset(200);

        List<Station> stationList = baseStationService.qryStationInfoDetailForHSF(params);

        //TODO 处理未完成
        ResponseStationList result = new ResponseStationList(0,"");
        result.setErrMsg("get station size too large , set default size 200") ;

        result.setStationList(stationList);
        return result;
    }

    /**
     * 根据组织机构编码(provinceOrgCode or cityorgcode or partorgcode)查询全部站列表
     * @param orgCode
     * @param offset
     * @param limit
     * @return
     */
    @Override
    public ResponseStationList listByOrg(String orgCode, int offset, int limit){
        ResponseStationList result = new ResponseStationList(0,"");

        BaseStationCriteria params = new BaseStationCriteria();
        params.setOrgCode(orgCode);

        if(limit > 100){
            result.setErrMsg("get station size too large , set default size 100") ;
            limit = 100 ;
        }
        params.setLimit(limit);
        params.setOffset(offset);
        List<Station> stationList = baseStationService.qryStationInfoDetailForHSF(params);

        result.setStationList(stationList);
        return result;
    }

    /**
     * 根据组织机构编码(provinceOrgCode or cityorgcode or partorgcode)获取所有本机构下的站数
     * @param orgCode
     * @return
     */
    @Override
    public ResponseInt countByOrg(String orgCode){
        BaseStationCriteria params = new BaseStationCriteria();
        params.setOrgCode(orgCode);

        int cnt = baseStationService.qryStationInfoCnt(params);
        ResponseInt result = new ResponseInt(0,"");
        result.setValue(cnt);

        return result;
    }

    /**
     * 统计所有站数
     * @return
     */
    @Override
    public ResponseInt totalCount(){
        BaseStationCriteria params = new BaseStationCriteria();

        int cnt = baseStationService.qryStationInfoCnt(params);
        ResponseInt result = new ResponseInt(0,"");
        result.setValue(cnt);
        return result;
    }

    /**
     * 统计所有站列表
     * @param offset
     * @param limit
     * @return
     */
    @Override
    public ResponseStationList list(int offset, int limit){
        ResponseStationList result = new ResponseStationList(0, "") ;
        if(limit > 100){
            result.setErrMsg("get station size too large , set default size 100") ;
            limit = 100 ;
        }
        BaseStationCriteria params = new BaseStationCriteria();
        params.setLimit(limit);
        params.setOffset(offset);

        List<Station> stationList = baseStationService.qryStationInfoDetailForHSF(params);

        result.setStationList(stationList);
        return result;
    }

    /**
     * 根据省份行政编码(provincecode)统计该省的充电站数量
     * @param provinceCode
     * @return
     */
    @Override
    public ResponseInt countByProvince(String provinceCode){
        BaseStationCriteria params = new BaseStationCriteria();
        params.setProvinceCode(provinceCode);
        int cnt = baseStationService.qryStationInfoCnt(params);
        ResponseInt result = new ResponseInt(0,"");
        result.setValue(cnt);
        return result;
    }

    /**
     * 根据省份行政编码(provincecode)统计该省的充电站列表
     * @param provinceCode
     * @param offset
     * @param limit
     * @return
     */
    @Override
    public ResponseStationList listByProvince(String provinceCode, int offset, int limit){
        ResponseStationList result = new ResponseStationList(0, "") ;
        if(limit > 100){
            result.setErrMsg("get station size too large , set default size 100") ;
            limit = 100 ;
        }
        BaseStationCriteria params = new BaseStationCriteria();
        params.setProvinceCode(provinceCode);
        params.setLimit(offset);
        params.setOffset(limit);
        List<Station> list = baseStationService.qryStationInfoDetailForHSF(params);
        result.setStationList(list);
        return result;

    }
    /**
     * 根据区域行政编码(districtCode)统计该区域内正常运⾏状态(stationstaus)的国⽹(isthird)充电站数量
     * @param districtCode
     * @return
     */
    @Override
    public ResponseInt countByDistrict(String districtCode){
        BaseStationCriteria params = new BaseStationCriteria();
        params.setDistrictCode(districtCode);
        List<Integer> stationStatusList = new ArrayList<Integer>();
        stationStatusList.add(BasicDataRedisConstants.STATION_STATUS_OPERATION);
        params.setStationstatus(stationStatusList);
        params.setIsthrids(BasicDataRedisConstants.THIRD_SGCC);
        int cnt = baseStationService.qryStationInfoCnt(params);
        ResponseInt result = new ResponseInt(0,"");
        result.setValue(cnt);
        return result;
    }

    /**
     * 根据区域行政编码(districtCode)统计该区域内正常运⾏状态(stationstaus)的国⽹(isthird)充电站列表
     * @param districtCode
     * @param offset
     * @param limit
     * @return
     */
    @Override
    public ResponseStationList listByDistrict(String districtCode, int offset, int limit){
        ResponseStationList result = new ResponseStationList(0, "") ;
        if(limit > 100){
            result.setErrMsg("get station size too large , set default size 100") ;
            limit = 100 ;
        }

        BaseStationCriteria params = new BaseStationCriteria();
        params.setDistrictCode(districtCode);
        params.setLimit(offset);
        params.setOffset(limit);

        List<Integer> stationStatusList = new ArrayList<Integer>();
        stationStatusList.add(BasicDataRedisConstants.STATION_STATUS_OPERATION);
        params.setStationstatus(stationStatusList);
        params.setIsthrids(BasicDataRedisConstants.THIRD_SGCC);

        List<Station> list = baseStationService.qryStationInfoDetailForHSF(params);
        result.setStationList(list);
        return result;

    }


    /**
     * 根据uuid获取站信息
     * @param uuid
     * @return
     */
    @Override
    public ResponseStation get(String uuid){
        ResponseStation result = new ResponseStation(0,"");
        result.setStation(baseStationService.findByUuidForHsf(uuid));
        return result;
    }

    /**
     * 统计站下运营商桩数量
     * 废弃方法，后续不再维护
     * @param stationUuid
     * @return
     */
    @Deprecated
    @Override
    public ResponseStation countByOperator(String stationUuid){
        ResponseStation result = new ResponseStation(0,"");
        if(StringUtils.isEmpty(stationUuid)){
            result.setErrCode(-1);
            result.setErrMsg("station uuid is blank");
            return result;
        }
        Map<String, Object> operatorCount = baseStationService.qryStationStakeCntGroupByOperator(stationUuid);
        ResponseObject object = new ResponseObject();
        object.setObject(operatorCount);
        result.setObject(object);
        return result;
    }

    /**
     * 废弃方法，实现类不处理
     * @param station
     * @return
     */
    @Deprecated
    @Override
    public ResponseBase set(Station station){
        return null;
    }

    /**
     * 废弃方法，实现类不处理
     * @param uuid
     * @return
     */
    @Deprecated
    @Override
    public ResponseBase delete(String uuid){
        return null;
    }

    /**
     * 废弃方法，实现类不处理
     * @return
     */
    @Deprecated
    @Override
    public boolean initStationInRedis(){
        return false;
    }
}
